from flask import Blueprint, request, jsonify, make_response, Response
from flask_cors import cross_origin
from app.apis.login_api import login
from app import token_required
from app.apis.login_api import wx_signature
from app.apis.login_api import generate_url_link as url_link
from app.apis.login_api import wechat_callback

wx_db = Blueprint('wx', __name__)


@wx_db.route('/wxsignature', methods=['POST'])
@cross_origin(supports_credentials=True, origins=["http://localhost:8081", "https://v1.miaotongchefu.cn/v3"])
def signature():
    return wx_signature()


@wx_db.route('/mp-url', methods=['GET'])
@cross_origin(supports_credentials=True, origins=["http://localhost:8081", "https://v1.miaotongchefu.cn/v3"])
def generate_url_link():
    return url_link()


@wx_db.route('/login', methods=['POST'])
def root_login():
    return login()


@wx_db.route('/verifyToken', methods=['GET'])
@token_required
def verifyToken():
    pass


@wx_db.route('/glogin', methods=['POST'])
@cross_origin(supports_credentials=True, origins=["http://localhost:8081", "https://v1.miaotongchefu.cn/v3"])
def glogin():
    if request.method == 'OPTIONS':
        resp = make_response()
        resp.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin')
        resp.headers['Access-Control-Allow-Headers'] = 'Content-Type,Authorization,X-Requested-With'
        resp.headers['Access-Control-Allow-Methods'] = 'GET,PUT,POST,DELETE,OPTIONS,PATCH'
        resp.headers['Access-Control-Allow-Credentials'] = 'true'
        return resp

    return wechat_callback()


@wx_db.route("/MP_verify_iKFzaT36A8ciljdv.txt")
def verify():
    return Response('iKFzaT36A8ciljdv', content_type="text/plain")
